package org.game.util;

import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import org.game.quartz.QuartzCounter;
import org.game.quartz.QuartzRefresher;
import org.game.quartz.QuartzUpdater;
import org.quartz.TriggerUtils;
import org.quartz.impl.triggers.CronTriggerImpl;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Quartz工具
 * <p>
 * Created by fxf on 2017/8/3.
 */
public class QuartzUtils {

    static Log log = LogFactory.get(QuartzUtils.class);

    /**
     * 刷新或重置
     * <p>
     * 在不被quartz管理的时候刷新或重置，例如开服和上线
     *
     * @param cron      cron时间表达式
     * @param refresher 被刷新对象
     */
    public static void outQuartzRefresh(String cron, QuartzRefresher refresher) {

        if (cron != null) {
            CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
            try {
                cronTriggerImpl.setCronExpression(cron);
                Date lastRefreshTime = refresher.getLastRefreshTime();
                Date nowDate = new Date();
                if (lastRefreshTime == null || lastRefreshTime.after(nowDate)) {
                    return;
                }
                List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, refresher.getLastRefreshTime(), nowDate);
                //TriggerUtils会把上次刷新的那个点也算进去，所以用>1
                if (dates.size() > 1) {
                    Date lastTime = dates.get(dates.size() - 1);
                    refresher.setLastRefreshTime(lastTime);
                    refresher.quartzRefresh(lastTime);
                    refresher.update();
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    }


    /**
     * 增加次数
     * <p>
     * 在不被quartz管理的时候更新，例如开服和上线
     *
     * @param cron cron时间表达式
     */
    public static void outQuartzUpdate(String cron, QuartzUpdater updater) {

        if (cron != null) {
            CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
            try {
                cronTriggerImpl.setCronExpression(cron);
                Date lastUpdateTime = updater.getLastUpdateTime();
                Date nowDate = new Date();
                if (lastUpdateTime == null || lastUpdateTime.after(nowDate)) {
                    return;
                }
                List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, lastUpdateTime, nowDate);
                //TriggerUtils会把上次刷新的那个点也算进去，所以用 > 1
                if (dates.size() > 1) {
                    for (int i = 1; i < dates.size(); i++) {
                        updater.enterGameUpdate();
                    }
                    Date lastTime = dates.get(dates.size() - 1);
                    updater.setLastUpdateTime(lastTime);
                    updater.update();
                }
            } catch (ParseException e) {
                e.printStackTrace();
                log.error("cron 表达式错误，");
            }
        }
    }
}
